package com.msh.platform.elm.service.impl;

import com.msh.platform.bizmedia.base.exception.BusinessException;
import com.msh.platform.bizmedia.base.util.Tools;
import com.msh.platform.elm.dao.ShopCartItemRepository;
import com.msh.platform.elm.dao.WxPubNoRepository;
import com.msh.platform.elm.entity.ShopCartItem;
import com.msh.platform.elm.entity.WxPubNo;
import com.msh.platform.elm.service.ShopCartItemService;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopCartItemServiceImpl implements ShopCartItemService {

    @Autowired
    private ShopCartItemRepository shopCartItemRepository;
    @Autowired
    private WxPubNoRepository wxPubNoRepository;
    
    public void addShopCartItem(int buyerId,int wxPubNoId) {
    	Integer currTime = Tools.getCurrTime();
    	WxPubNo wxPubNo = wxPubNoRepository.findById(wxPubNoId)
    			.orElseThrow(() -> new BusinessException(-1,"根据公众号ID没有获取到数据,id:"+wxPubNoId));
    	
    	ShopCartItem shopCartItem = new ShopCartItem();
    	shopCartItem.setBuyerId(buyerId);
    	shopCartItem.setSellerId(wxPubNo.getSellerId());
    	shopCartItem.setPubNoId(wxPubNoId);
    	shopCartItem.setStateTime(currTime);
    	shopCartItemRepository.save(shopCartItem);
    }
    
    public void delShopCartItem(String itemId) {
    	shopCartItemRepository.deleteById(itemId);
    }
    
    public List<ShopCartItem> queryCartItem(int buyerId) {
    	List<ShopCartItem> itemList = shopCartItemRepository.findByBuyerId(buyerId);
    	return itemList;
    }

}
